<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true" class="nav-custom">
			<!-- <block slot="backText">返回</block> -->
			<block slot="content">天气综述</block>
		</cu-custom>
		<view class="app-box" :style="{ height: appHeight + 'px' }">
			<u-tabs
				active-color="#fff"
				inactive-color="#fff"
				bg-color="#2695FE"
				style="height: 80rpx"
				:bold="false"
				font-size="28"
				:list="list"
				:is-scroll="false"
				:current="current"
				@change="changeTabs"
			></u-tabs>
			<view class="app-box-content">
				<!-- {{ fileContent }} -->
				<!-- <iframe :src="fileContent" frameborder="0"></iframe> -->
				<!-- <web-view :src="fileContent" /> -->
				<!-- <u-parse :html="fileContent"></u-parse> -->
				<text>{{ fileContent }}</text>
				<!-- <view id="pdf-content"></view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import moment from 'moment'
	import weather from '@/api/monitor/weather.js'
	import Pdfh5 from 'pdfh5'
	function decodeGB2312BinaryFile(binaryData) {
		return new Promise((resolve, reject) => {
			const blob = new Blob([binaryData])

			const reader = new FileReader()
			reader.onload = function (event) {
				const arrayBuffer = event.target.result

				const textDecoder = new TextDecoder('gb2312')
				const decodedText = textDecoder.decode(arrayBuffer)

				resolve(decodedText)
			}

			reader.onerror = function (event) {
				reject(event.target.error)
			}

			reader.readAsArrayBuffer(blob)
		})
	}
	export default {
		data() {
			return {
				appHeight: 0,
				current: 0,
				fileContent: '',
				list: [
					{ name: '三明市综述', value: '1' },
					{ name: '福建省综述', value: '2' }
				],
				src: ''
			}
		},
		async mounted() {
			this.appHeight = await this.$getHeight('.nav-custom')
			this.statPage()
		},
		methods: {
			async statPage() {
				try {
					// uni.showLoading({
					// 	title: '正在加载'
					// })
					let code = ''
					if (this.current == 0) {
						code = '每日天气综述(三明)'
					} else {
						code = '每日天气综述(福建)'
					}
					let params = {
						TenantId: 142307070918780,
						ProductTypeCode: code
					}
					const resp = await weather.weather(params)
					if (resp.success) {
						let arr = resp.data.rows
							.map((item) => {
								item.cTime = moment(item.cTime).format('MM月DD日 HH:mm')
								return item
							})
							.reverse()
						if (arr.length > 0) {
							let obj = {
								title: arr[0].title,
								type: 'url',
								url: arr[0].filePath //filePath
							}
							this.initUrl(obj.url)
						}
					} else {
					}
				} catch (error) {}
			},
			initUrl(url) {
				setTimeout(() => {
					// this.fileContent = 'http://175.44.138.53:5566/' + url
					uni.request({
						url: url, // 替换为你的txt文件URL
						responseType: 'arraybuffer', // 设置响应类型为arraybuffer
						header: {
							'Content-Type': 'application/x-www-form-urlencoded;charset=gbk'
						},
						success: async (res) => {
							decodeGB2312BinaryFile(res.data)
								.then((decodedText) => {
									this.fileContent = decodedText
								})
								.catch((error) => {
									console.error('解析文件失败:', error)
								})
						},
						fail: (error) => {
							console.error('加载文本文件失败:', error)
						}
					})
					// this.pdfh5 = new Pdfh5('#pdf-content', {
					// 	// data: res // pdf文件流
					// 	pdfurl: 'http://175.44.138.53:5566/' + url
					// })
				}, 100)
			},
			changeTabs(index) {
				this.current = index
				this.statPage()
			}
		}
	}
</script>

<style lang="less" scoped>
	.app-box {
		width: 100%;

		.app-box-content {
			height: calc(100% - 80rpx);
			padding: 10px;
			overflow: auto;

			text {
				font-size: 15px;
				font-family: '微软雅黑';
			}
		}
	}
</style>
